package com.HelloWidget;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

public class VimeoVideoOfTheDay extends VideoContentDescriptor {

	private Element _root;

	public  void init () throws IOException {

			_doc = Jsoup.connect("http://vimeo.com/channels/20465").get();
			_root = _doc.getElementById("brozar_clips");
			//System.out.println("_root1:"+_root.html());
			//Elements elems = _root.getElementsByClass("last");
			//_root = elems.first();
			//System.out.println("_root2:"+_root.html());
			_initialized = true;

	}
		
	@Override
	public CharSequence getHeadline() {
		Element img = _root.getElementsByClass("title").first();
		//System.out.println("img a 1st:"+img.toString());
		img = img.getElementsByTag("a").first();
		//System.out.println("img a 1st:"+img.toString());
		String attr = img.text();
		// System.out.println("img a 1st:"+img.toString());
	
		return attr; //after ':' there is a space
	}

	@Override
	public CharSequence getDescription() {
		Element img = _root.getElementsByClass("byline").first();
		img = img.getElementsByTag("a").first();
		String attr = img.text();
		// System.out.println("img a 1st:"+img.toString());
	
		return "by "+attr; 
	}

	@Override
	public String getBitmapUrl() {
		Element img = _root.getElementsByTag("img").first();
		// System.out.println("img a 1st:"+img.toString());
		int start = img.toString().indexOf("\"");
		int end = img.toString().indexOf("\"",start+1);
		//System.out.println("start:"+start+" end:"+end);

		return  img.toString().substring(start+1, end);
	}

	@Override
	public String getUrl() {
		Element img = _root.getElementsByClass("title").first().getElementsByTag("a").first();
		String attr = img.attr("href");
		// System.out.println("img a 1st:"+img.toString());
	
		return "http://vimeo.com"+attr;
	}

}
